package com.yowasa.xiuxian.entity.enums;

import com.google.common.collect.Maps;
import com.yowasa.xiuxian.util.RandomUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

@Getter
@AllArgsConstructor
public enum ElementEnum {
    GOLD("金"),
    WOOD("木"),
    WATER("水"),
    FIRE("火"),
    SOIL("土"),
    ;
    //名
    private String name;

    public static ElementEnum getByName(String name) {
        return Arrays.stream(ElementEnum.values()).filter(e -> e.getName().equals(name)).findFirst().orElse(null);
    }

    public boolean judgeAdjust(ElementEnum other) {
        if (other == null) {
            return true;
        }
        if (other != this.getAgainst() && other.getAgainst() != this) {
            return true;
        }
        return false;
    }

    public boolean judgeAdjust(String name) {
        ElementEnum o = getByName(name);
        return judgeAdjust(o);
    }

    /**
     * 获取激活灵根
     *
     * @param map      属性
     * @param activity 激活阈值
     * @return
     */
    public static String getLingGenDesc(Map<String, Integer> map, int activity) {
        Map<ElementEnum, Integer> result = Maps.newHashMap();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            result.put(getByName(entry.getKey()), entry.getValue());
        }
        String desc = map.entrySet().stream().filter(e -> e.getValue().compareTo(activity) >= 0).sorted(Comparator.comparing(Map.Entry::getValue)).map(e -> e.getKey()).collect(Collectors.joining());
        return new StringBuffer(desc).reverse().toString();
    }


    /**
     * 获取枚举映射
     *
     * @param map
     * @return
     */
    public static Map<ElementEnum, Integer> getLingGenDTO(Map<String, Integer> map) {
        Map<ElementEnum, Integer> result = Maps.newHashMap();
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            result.put(getByName(entry.getKey()), entry.getValue());
        }
        return result;
    }


    /**
     * 生成随机灵根
     *
     * @return
     */
    public static Map<String, Integer> genLingGen() {
        List<Integer> r5 = RandomUtil.intSplit(100, 1, 5);
        Collections.shuffle(r5);
        Map<String, Integer> eleMap = Maps.newHashMap();
        for (int i = 0; i < ElementEnum.values().length; i++) {
            eleMap.put(ElementEnum.values()[i].getName(), r5.get(i));
        }
        return eleMap;
    }

    public ElementEnum getAgainst() {
        RelateEnum relateEnum = RelateEnum.getByName(this);
        return relateEnum.getAgainst();
    }

    public ElementEnum getGenerate() {
        RelateEnum relateEnum = RelateEnum.getByName(this);
        return relateEnum.getGenerate();
    }


    @Getter
    @AllArgsConstructor
    public enum RelateEnum {
        GOLD("金", ElementEnum.GOLD, ElementEnum.WOOD, ElementEnum.WATER),
        WOOD("木", ElementEnum.WOOD, ElementEnum.SOIL, ElementEnum.FIRE),
        WATER("水", ElementEnum.WATER, ElementEnum.FIRE, ElementEnum.WOOD),
        FIRE("火", ElementEnum.FIRE, ElementEnum.GOLD, ElementEnum.SOIL),
        SOIL("土", ElementEnum.SOIL, ElementEnum.WATER, ElementEnum.GOLD),
        ;
        //名
        private String name;

        //名
        private ElementEnum self;
        //克
        private ElementEnum against;
        //生
        private ElementEnum generate;

        public static RelateEnum getByName(ElementEnum ele) {
            return Arrays.stream(RelateEnum.values()).filter(e -> e.getSelf() == ele).findFirst().orElse(null);
        }


    }


}
